<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: SlideShowPortlet.php 471 2011-01-25 17:27:01Z steen.rabol $
 *
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Common.Data.PortalConfigRecord');
Prado::using('Application.Portlets.SlideShowPortlet.Common.SlideShowPortletRecord');

class SlideShowPortlet extends Portlet
{
	private $_savePath;
	private $_width;
	private $_height;
	private $_delay;

	public function  onInit($p)
	{
		parent::onInit($p);
		
		$cRow	= $this->ConfigRecord;
		if(!$cRow)
		{
			$value			= Pardo::serialize(array(	"width" => "500",
												"height" => "150",
												"delay" => "4"));
			$cRow			= new PortalConfigRecord();
			$cRow->name		= $this->ConfigName;
			$cRow->value	= $value;
			$cRow->save();
			$cRow	= PortalConfigRecord::finder()->findByname($name);
		}

		$this->_savePath	= $this->Application->PortalBasePath . '/storage/SlideShowPortlet/' . $this->Parameters['id'];
		PortalUtil::CreateDirStructure($this->_savePath);

		// Set our values
		$value = Prado::unserialize($cRow->value);
		$this->Width	= $value['width'];
		$this->Height	= $value['height'];
		$this->Delay	= $value['delay'];
	}

	public function onLoad($p)
	{
		// display files;
		$criteria=new TActiveRecordCriteria();
		$criteria->OrdersBy['displayorder']='asc';
		$criteria->Condition = "pid=:pid";
		$criteria->Parameters[':pid'] = $this->Parameters['id'];

		$this->dtlistSlideShowFiles->DataSource = SlideShowPortletRecord::finder()->findAll($criteria);
		$this->dtlistSlideShowFiles->dataBind();
		$this->FileUpload->MaxFileSize			= PortalUtil::return_bytes(ini_get('upload_max_filesize'));
		$this->ltFileSize->Text					= ini_get('upload_max_filesize');
		$this->tbSlideShowWidth->Text			= $this->Width;
		$this->tbSlideShowHeight->Text			= $this->Height;
		$this->tbSlideShowDelay->Text			= $this->Delay;
	}

	protected function getConfigName()
	{
		return get_class() . '_' . $this->Parameters['id'];
	}

	public function OnFileUpload($sender,$param)
	{
		if($sender->HasFile)
		{
			$typeAccepted = array("image/jpeg", "image/gif", "image/png");
			if(in_array($sender->FileType,$typeAccepted))
			{
				$key = array_search($sender->FileType, $typeAccepted);
				$newfilename	= PortalUtil::getGUID();
				switch($key)
				{
					case 0: $newfilename .= '.jpg'; break;
					case 1: $newfilename .= '.gif'; break;
					case 2: $newfilename .= '.png'; break;
				}

				$filename = PortalUtil::PathName($this->_savePath) . $newfilename;

				$ret = $sender->saveAs($filename);

				$dFile			= new SlideShowPortletRecord();

				$dFile->pid			= $this->Parameters['id'];
				$dFile->filename	= $newfilename;
				$dFile->filesize	= $sender->FileSize;
				$dFile->filedate	= time();
				$dFile->displayorder	= 999;
				$dFile->save();

				// Renumber
				$this->RenumDisplayOrder();
			}

			$this->Page->gotoPage($this->Parameters['pagename'],array("PageMode" => $this->Page->EditMode));
		}
	}

	public function onItemCommand($s,$p)
	{
		switch($p->CommandName)
		{
			case "MoveDown":	$this->MoveDown($p->CommandParameter); break;
			case "MoveUp":	$this->MoveUp($p->CommandParameter); break;
		}

	}

	public function deleteSlideShowFile($sender,$param)
	{
		$id		= $this->dtlistSlideShowFiles->DataKeys[$param->Item->ItemIndex];
		$dFile	= SlideShowPortletRecord::finder()->findByid($id);

		if($dFile)
		{
			if(file_exists($this->_savePath . '/' . $dFile->filename))
			{
				@unlink($this->_savePath . '/' . $dFile->filename);
			}
			else
			{
				PortalUtil::Log("File '" .  $this->_savePath . '/' . $dFile->filename . "' does not exists");
			}
			
			$dFile->delete();
		}

		$this->Page->gotoPage($this->Parameters['pagename'],array("PageMode" => $this->Page->EditMode));
	}

	public function SaveContent($sender,$param)
	{
		$value			= Prado::serialize(array(	"width"		=> $this->tbSlideShowWidth->Text,
													"height"	=> $this->tbSlideShowHeight->Text,
													"delay"		=> $this->tbSlideShowDelay->Text));
		$this->ConfigRecord->value = $value;
		$this->ConfigRecord->save();

		$this->Page->gotoPage($this->Parameters['pagename'],array("PageMode" => $this->Page->EditMode));
	}

	public function getWidth()
	{
		return $this->_width;
	}
	
	public function setWidth($value)
	{
		$this->_width	= TPropertyValue::ensureInteger($value);
	}

	public function getHeight()
	{
		return $this->_height;
	}

	public function setHeight($value)
	{
		$this->_height	= TPropertyValue::ensureInteger($value);
	}

	public function getDelay()
	{
		return $this->_delay;
	}
	
	public function setDelay($value)
	{
		$this->_delay = TPropertyValue::ensureInteger($value);
	}

	
	public function getImageUrl($value)
	{
		return $fileurl = $this->Application->PortalBaseUrl . 'storage/SlideShowPortlet/' . $this->Parameters['id'] . '/' . $value;
	}

	public function MoveUp($id)
	{
		$row = SlideShowPortletRecord::finder()->findByid_AND_pid($id,$this->Parameters['id']);

		if($row)
		{
			$row->displayorder	-= 1.001;
			$row->save();
			$this->RenumDisplayOrder();
			$this->RenumDisplayOrder(1);
		}

		$this->Page->gotoPage($this->Parameters['pagename'],array("PageMode" => $this->Page->EditMode));
	}

	public function MoveDown($id)
	{
		$row = SlideShowPortletRecord::finder()->findByid_AND_pid($id,$this->Parameters['id']);
		if($row)
		{
			$row->displayorder	+= 1.001;
			$row->save();
			$this->RenumDisplayOrder();
			$this->RenumDisplayOrder(1);
		}

	$this->Page->gotoPage($this->Parameters['pagename'],array("PageMode" => $this->Page->EditMode));
	}

	public function RenumDisplayOrder($p_startat = 999.000)
	{
		$newdisplayorder	= $p_startat;
		//$records 			= SlideShowPortletRecord::finder()->findAllBypid($this->Parameters['id']);
		
		$criteria=new TActiveRecordCriteria();
		$criteria->OrdersBy['displayorder']='asc';
		$criteria->Condition = "pid=:pid";
		$criteria->Parameters[':pid'] = $this->Parameters['id'];

		$records 			= SlideShowPortletRecord::finder()->findAll($criteria);

		foreach($records as $record)
		{
			$record->displayorder = $newdisplayorder;
			$record->save();
			$newdisplayorder++;
		}
	}

}
?>